package session.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.Supplier;
import entity.Address;
import entity.RawMaterial;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;

@Stateless
public class SupplierSessionBean implements SupplierSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;
    private Supplier supplier;
    
    @Override
    public void deleteSupplier(String supplierId) {
        Supplier s = entityManager.find(Supplier.class, supplierId);
        
        entityManager.remove(s);
        entityManager.flush();
    }
     
    @Override
    public void deleteSupplierRawMaterial(String supplierId, Long rawMaterialId) {
        Supplier s = entityManager.find(Supplier.class, supplierId);
        RawMaterial temp = entityManager.find(RawMaterial.class, rawMaterialId);
        s.getRawMaterials().remove(temp);
        entityManager.persist(s);
    }

    @Override
    public void addSupplier(String supplierId, String name, Integer contact, Address supplierAddress, String ranking, String comment, String email, String company, List<RawMaterial> rawMaterials) {
        Address sAddress = new Address(supplierAddress.getStreet(), supplierAddress.getCountry(), supplierAddress.getCity(), supplierAddress.getPostal(), "Supplier");

        supplier = new Supplier(supplierId, name, contact, sAddress, ranking, comment, email, company, rawMaterials);
        
        entityManager.persist(supplier);
        entityManager.flush();
    }

    @Override
    public void updateSupplier(String supplierId, String name, Integer contact, Address supplierAddress, String ranking, String comment, String email, String company) {
        try {
            Supplier tempSupplier = getSupplierBySupId(supplierId);
            
            if (tempSupplier != null) {
                tempSupplier.setName(name);
                tempSupplier.setContact(contact);
                tempSupplier.setEmail(email);
                tempSupplier.setRanking(ranking);
                tempSupplier.setComment(comment);
                tempSupplier.setCompany(company);

                Address sAdd = tempSupplier.getSupplierAddress();
                sAdd.setCity(supplierAddress.getCity());
                sAdd.setCountry(supplierAddress.getCountry());
                sAdd.setPostal(supplierAddress.getPostal());
                sAdd.setStreet(supplierAddress.getStreet());
            }
            
            entityManager.persist(tempSupplier);
            entityManager.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void updateSupplierRawMaterial(String supplierId, RawMaterial rawMaterial) {

        Supplier s = entityManager.find(Supplier.class, supplierId);
        System.out.println("s.getId(): " + s.getSupplierId());
        List<RawMaterial> temp = s.getRawMaterials();
        System.out.println("temp.size(): " + temp.size());
        RawMaterial temp2 = entityManager.find(RawMaterial.class, rawMaterial.getId());
        System.out.println("HERE 1");
        temp.add(temp2);

        s.setRawMaterials(temp);
        System.out.println("HERE 2");
        entityManager.persist(s);
        System.out.println("HERE 3");
        //entityManager.flush();
    }

    @Override
    public Supplier getSupplierBySupId(String supplierId, String company) {
        Query query = entityManager.createQuery("SELECT s FROM Supplier s");
        
        for (Object o : query.getResultList()) {
            Supplier s = (Supplier) o;
            
            if (s.getSupplierId().equals(supplierId) || s.getCompany().equals(company)) {
                return s;
            }
        }
        return null;
    }

    @Override
    public Supplier getSupplierBySupId(String supplierId) {
        Supplier s = entityManager.find(Supplier.class, supplierId);

        return s;
    }

    @Override
    public List<Supplier> getSuppliers() {
        Query query = entityManager.createQuery("SELECT s FROM Supplier s");
        ArrayList<Supplier> suppliers = new ArrayList<Supplier>();

        for (Object o : query.getResultList()) {
            Supplier s = (Supplier) o;
            suppliers.add(s);
        }

        return suppliers;
    }
}
